;
(function() {
	var skill = {
		//设定某家店铺内用户的固定推荐者
		setBusRecommender: function(bus_id, recommender_id) {
			var recommender_map = this.recommender_map || (this.recommender_map = {});
			if (!recommender_map.hasOwnProperty(bus_id)) {
				var check_user_id = recommender_id;
				//上级不可反转为下级，逐级遍历上级
				while (check_user_id) {
					check_user_id = recommender_map[check_user_id];
					if (check_user_id == user_id) {
						//throwCusError("上级不可反转为下级")
						return
					}
				}
				recommender_map[bus_id] = recommender_id
			} else {
				//throwCusError("已经有绑定的关系了")
			}
		},
		//获取某家店铺内用户的固定推荐者
		getBusRecommender: function(bus_id) {
			var recommender_map = this.recommender_map || {};
			return recommender_map[bus_id]
		},
		_getBinaAchList: function() {
			var bina_ach_list = this.bina_ach_list;
			if (!bina_ach_list) {
				bina_ach_list = this.bina_ach_list = []
				this.save();
			}
			return bina_ach_list;
		},
		//获取二进制兑现信息
		_getBinaExchangeAcc: function() {
			var bina_exchange_acc = this.bina_exchange_acc;
			if (!bina_exchange_acc) {
				bina_exchange_acc = this.bina_exchange_acc = {};
				/*
					[bus_id]:{
						exchanged_acc://已经兑换的额度
						apply_acc://正在申请兑换的额度
						state://兑换状态
					}
				*/
				this.save();
			}
			return bina_exchange_acc;
		},
		_getBinaExchangeAccBuyBusId: function(bus_id) {
			var bina_exchange_acc = this._getBinaExchangeAcc();
			var exchange_acc = bina_exchange_acc[bus_id] || {};
			exchange_acc = {
				exchanged_acc: exchange_acc.exchanged_acc || 0,
				apply_acc: exchange_acc.apply_acc || 0,
				state: exchange_acc.state || BinaCon.STATE.未申请兑换
			}
			return exchange_acc;
		},
		//非安全
		_setBinaExchangeAccBuyBusId: function(bus_id, exchange_acc) {
			var bina_exchange_acc = this._getBinaExchangeAcc();
			bina_exchange_acc[bus_id] = exchange_acc;
			this.save();
		},
		//代码片段，获取全部商品二进制订单信息
		_getBinaBus: function(bus_id) {
			var user_id = this._id;
			var bina_ach_list = this._getBinaAchList();
			// console.log("bina_ach_list:", bina_ach_list);
			var result = [];
			var bus_map = {};
			var self = this;
			bina_ach_list.forEach(function(bina_id) {
				var bina;
				if (bina_id && (bina = BinaCon.get(bina_id))) {
					var bus_id = BinaCon.getBusId(bina_id);
					var bus_bina_list = bus_map[bus_id] || (bus_map[bus_id] = [])
					var buyer_info = bina.getUser(user_id);
					bus_bina_list.push(buyer_info)
				}
			});
			//只取指定的商家信息
			if (bus_id) {
				var new_bus_map = {};
				new_bus_map[bus_id] = bus_map[bus_id];
				bus_map = new_bus_map;
			}
			Object.keys(bus_map).forEach(function(bus_id) {
				var bus_bina_list = bus_map[bus_id] || [];
				var total_first_acc = 0;
				var total_second_acc = 0;
				var total_third_acc = 0;
				var total_bina_cash = 0;
				// console.log("bus_bina_list:", bus_id, bus_bina_list);
				bus_bina_list.forEach(function(bina_user_info) {
					total_first_acc += bina_user_info.first_acc || 0;
					total_second_acc += bina_user_info.second_acc || 0;
					total_third_acc += bina_user_info.third_acc || 0;
					if (Array.isArray(bina_user_info.achs)) {
						var _total_bina_cash = 0;
						bina_user_info.achs.forEach(function(ach_info) {
							_total_bina_cash += ach_info.bina_cash
						});
						total_bina_cash += _total_bina_cash;
					}
				});
				var exchange_acc = self._getBinaExchangeAccBuyBusId(bus_id);
				var bus_user = BusUserCon.get(BusUserCon.build({
					user_id: self._id,
					bus_id: bus_id
				}));
				result.push({
					bus_id: bus_id,
					// bus: BusCon.get(bus_id),
					bus_bina_list: bus_bina_list,
					total_first_acc: total_first_acc,
					total_second_acc: total_second_acc,
					total_third_acc: total_third_acc,
					total_bina_cash: total_bina_cash,
					exchange_acc: exchange_acc,
					bina_acc_to_money: bus_user.bina_acc_to_money,
					can_apply_acc: (total_first_acc + total_second_acc + total_third_acc - exchange_acc.exchanged_acc - exchange_acc.apply_acc - bus_user.bina_acc_to_money).fixMoney(),
				});
			});
			return result;
		},
		//带上商家信息
		//bus_id为空时，返回所有列表
		getBinaBus: function(bus_id) {
			var result = this._getBinaBus(bus_id);
			result.forEach(function(info) {
				info.bus = BusCon.get(info.bus_id)
			});
			return result;
		},
		//获取用户指定商家的商品二进制信息
		getBinaBusByBusId: function(bus_id) {
			var result = this._getBinaBus(bus_id)[0];
			result.bus = BusCon.get(result.bus_id);
			return result;
		},
		//获取用户指定商家的商品二进制信息的一级分销的人的信息
		getBinaFirstAccUsersByBusId: function(bus_id) {
			// var user_id = this._id;
			// //获“取分销订单”列表，每个针对一个商品
			// var bina_ach_list = this._getBinaAchList();
			// var user_bina_map = {};
			// bina_ach_list.forEach(function(bina_id) {
			// 	if (!bina_id) {
			// 		return
			// 	}
			// 	var _bus_id = BinaCon.getBusId(bina_id);
			// 	if (_bus_id !== bus_id) {
			// 		return;
			// 	}
			// 	var bina = BinaCon.get(bina_id);
			// 	//获取这个分销信息中的用户列表
			// 	console.log(bina._getUserInfoListByRecommender());
			// 	bina._getUserInfoListByRecommender().forEach(function(buyer_info) {
			// 		var achs = buyer_info.achs || [{
			// 			ach_id: buyer_info.ach_id,
			// 			goods_list: buyer_info.bina_goods_list,
			// 			bina_cash: buyer_info.bina_cash
			// 		}];
			// 		achs.forEach(function(ach_info) {
			// 			var user_id = AchCon.get(ach_info.ach_id).info.user_id;
			// 			var user_bina = user_bina_map[user_id];
			// 			if (!user_bina) {
			// 				user_bina = user_bina_map[user_id] = {
			// 					user: UserCon.get(user_id),
			// 					total_bina_cash: 0
			// 				}
			// 			}
			// 			user_bina.total_bina_cash += ach_info.bina_cash
			// 		});
			// 	});
			// });
			// var user_bina_list = Object.keys(user_bina_map).map(function(user_id) {
			// 	return user_bina_map[user_id]
			// });
			// return user_bina_list;

			var user_id = this._id;
			var user_bina_map = {};
			var bina_ach_list = this._getBinaAchList();
			// console.log("bina_ach_list:", bina_ach_list);
			var result = [];
			var bus_map = {};
			var self = this;
			bina_ach_list.forEach(function(bina_id) {
				var bina;
				if (bina_id && (bina = BinaCon.get(bina_id))) {
					var bus_id = BinaCon.getBusId(bina_id);
					var bus_bina_list = bus_map[bus_id] || (bus_map[bus_id] = [])
					var buyer_info = bina.getUser(user_id);
					bus_bina_list.push(buyer_info)
				}
			});
			//只取指定的商家信息
			if (bus_id) {
				var new_bus_map = {};
				new_bus_map[bus_id] = bus_map[bus_id];
				bus_map = new_bus_map;
			}
			Object.keys(bus_map).forEach(function(bus_id) {
				var bus_bina_list = bus_map[bus_id] || [];
				var total_first_acc = 0;
				var total_second_acc = 0;
				var total_third_acc = 0;
				bus_bina_list.forEach(function(bina_user_info) {
					(bina_user_info.first_bina_ach_list || []).forEach(function(ach_id) {
						var ach = AchCon.get(ach_id);
						var user_id = ach.info.user_id;
						(user_bina_map.hasOwnProperty(user_id) ? user_bina_map[user_id] : (user_bina_map[user_id] = {
							user: UserCon.get(user_id),
							total_first_cash: 0
						})).total_first_cash += ach._getAchCash();
					});
				});
			});
			Object.keys(user_bina_map).forEach(function(user_id) {
				result.push(user_bina_map[user_id]);
			});
			return result;
		},
		//非安全，添加商品二进制订单记录
		_addBinaAch: function(bina_id) {
			var bina_ach_list = this._getBinaAchList();
			if (bina_ach_list.indexOf(bina_id) === -1) {
				bina_ach_list.push(bina_id);
			}
			this.save();
		},
		bina_未申请兑换_to_申请兑换中: function(bus_id) {
			var exchange_acc = this._getBinaExchangeAccBuyBusId(bus_id);
			if (exchange_acc.state !== BinaCon.STATE.未申请兑换) {
				throwCusError("兑现所在状态无法进行相应转换")
			}
			exchange_acc.state = BinaCon.STATE.申请兑换中;
			//计算可兑换的钱，全部兑换
			var bina_bus = this._getBinaBus(bus_id)[0];
			exchange_acc.apply_acc = bina_bus.total_first_acc + bina_bus.total_second_acc + bina_bus.total_third_acc - exchange_acc.exchanged_acc - exchange_acc.apply_acc;
			this._setBinaExchangeAccBuyBusId(bus_id, exchange_acc);
		},
		bina_商家已付款_to_用户确认付款: function(bus_id) {
			var exchange_acc = this._getBinaExchangeAccBuyBusId(bus_id);
			if (exchange_acc.state !== BinaCon.STATE.商家已付款) {
				throwCusError("兑现所在状态无法进行相应转换")
			}
			exchange_acc.state = BinaCon.STATE.用户确认付款;
			//兑换完成，将兑换的钱
			exchange_acc.exchanged_acc += exchange_acc.apply_acc;
			exchange_acc.apply_acc = 0;
			this._setBinaExchangeAccBuyBusId(bus_id, exchange_acc);
		},
		bina_to_BusUser_money: function(bus_id, money) {
			var bus_user = BusUserCon.get(BusUserCon.build({
				user_id: this._id,
				bus_id: bus_id
			}));
			bus_user._addBinaMoney(money);
			var bina = this.getBinaBusByBusId(bus_id);
			return bina;
		}
	};
	module.exports = skill;
	var db = require("../db/index");
	var BinaCon = require("../bina/_con");
	var CouponCon = require("../coupon/_con");
	var DomainCon = require("../domain/_con");
	var GoodsCon = require("../goods/_con");
	var UserCon = require("../../core/user/_con");
	var BusCon = require("../../core/bus/_con");
	var AchCon = require("../ach/_con");
	var ClassifyCon = require("../bus.classify/_con");
	var TagCon = require("../bus.tag/_con");
	var LeagueCon = require("../league/_con");
	var BuffCon = require("../buff/_con");
	var BusUserBindCon = require("../bus_user_bind/_con");
	var BusUserCon = require("../bus_user/_con");
	var BusGuideCon = require("../bus.guide/_con");
	var CommentCon = require("../comment/_con");
	var config = require("../_include/config");
	var data_cut = require("../_include/data_cut");
	var helper = require("../_include/helper");
	var md5 = require("../_include/md5");
	var $Object = require("../_include/$.Object");
	var $Array = require("../_include/$.Array");
	var tld = require('tldjs');
	var fs = require("fs");
	var Fiber = require('fibers');
}());